<template>
  <div class="channel-edit">
    <van-cell title="我的频道" :border="false" class="title-text">
      <van-button
        size="mini"
        round
        type="danger"
        plain
        class="edit-btn"
        @click="isEdit = !isEdit"
        >{{ isEdit ? "完成" : "编辑" }}</van-button
      >
    </van-cell>
    <van-grid :gutter="10" class="my-grid">
      <van-grid-item
        class="grid-item"
        v-for="(channel, index) in myChannels"
        :key="index"
        @click="myChannelClick(channel, index)"
      >
        <van-icon
          slot="icon"
          name="clear"
          v-show="isEdit && !fixedChannels.includes(channel.id)"
        ></van-icon>
        <span class="text" slot="text" :class="{ active: index === active }">{{
          channel.name
        }}</span>
      </van-grid-item>
    </van-grid>
    <van-cell title="频道推荐" :border="false" class="title-text"></van-cell>
    <van-grid :gutter="10" class="recommend-channel">
      <van-grid-item
        class="grid-item"
        v-for="(channel, index) in recommendChannels"
        :key="index"
        icon="plus"
        :text="channel.name"
        @click="addChannel(channel)"
      />
    </van-grid>
  </div>
</template>

<script>
import {
  getAllChannels,
  addUserChannel,
  deleteUserChannel
} from '@/api/channels'
import { mapState } from 'vuex'
import { setItem } from '@/utils/storage'

export default {
  name: 'ChannelEdit',
  components: {},
  props: {
    myChannels: {
      type: Array,
      require: true
    },
    active: {
      type: Number,
      require: true
    }
  },
  data () {
    return {
      allChannels: [], // 所有频道
      isEdit: false, // 控制编辑按钮
      fixedChannels: [0], // 固定频道不允许被删除
      changemyChannels: this.myChannels // 解决不能改变props的myChannels
    }
  },
  computed: {
    ...mapState(['user']),

    recommendChannels () {
      return this.allChannels.filter((channel) => {
        return !this.changemyChannels.find((myChannel) => {
          return myChannel.id === channel.id
        })
      })
    }

    //   recommendChannels(){
    //       const channels = []
    //       this.allChannels.forEach(channel=>{
    //         const ret =  this.myChannels.find(myChannel=>{
    //               return myChannel.id === channel.id
    //           })
    //         // 如果我的频道不包括该频道,则收集到推荐频道中
    //           if(!ret){
    //               channels.push(channel)
    //           }
    //       })
    //       return channels
    //   }
  },
  watch: {},
  created () {
    this.loadAllChannels()
  },
  mounted () {},
  methods: {
    async loadAllChannels () {
      try {
        const { data } = await getAllChannels()
        this.allChannels = data.data.channels
      } catch (err) {
        this.$toast('获取数据失败')
      }
    },
    async addChannel (channel) {
      //  console.log(channel)
      this.changemyChannels.push(channel)

      //  处理持久化
      if (this.user) {
        // 已登录,把数据请求接口放到线上
        try {
          await addUserChannel({
            id: channel.id, // 频道ID
            seq: this.changemyChannels.length // 序号
          })
        } catch (err) {
          this.$toast('保存失败,稍后重试')
        }
      } else {
        // 未登录,把数据存储到本地
        setItem('TOUTIAO_CHANNELS', this.changemyChannels)
      }
    },

    myChannelClick (channel, index) {
      // console.log(channel)
      // 如果是编辑状态,点击则是删除; 非编辑状态,点击则是切换频道
      // 根据isEdit来判断
      if (this.isEdit) {
        if (this.fixedChannels.includes(channel.id)) {
          // 不允许删除固定的频道
          return
        }

        this.changemyChannels.splice(index, 1)

        if (index <= this.active) {
          // 让激活的索引-1
          this.$emit('update-active', index - 1, true)
        }
        this.deleteChannel(channel)
      } else {
        // props里数据不能修改;所以通过子传父通信
        // 参数true/false,控制isChannelEditShow关闭还是开启弹出层
        this.$emit('update-active', index, false)
      }
    },

    async deleteChannel (channel) {
      try {
        if (this.user) {
          // 已登录,则将数据更新到线上
          await deleteUserChannel(channel.id)
        } else {
          // 未登录,将数据更新到本地
          setItem('TOUTIAO_CHANNELS', this.changemyChannels)
        }
      } catch (err) {
        this.$toast('操作失败,稍后重试')
      }
    }
  }
}
</script>

<style scoped lang="less">
.channel-edit {
  padding: 85px 0;
  .title-text {
    font-size: 32px;
    color: #333333;
  }
  .edit-btn {
    width: 104px;
    height: 48px;
    font-size: 26px;
    color: #f85958;
    border: 1px solid #f85958;
  }
  .grid-item {
    width: 160px;
    height: 86px;
    /deep/ .van-grid-item__content {
      white-space: nowrap;
      background-color: #f5f5f6;
      .van-grid-item__text,
      .text {
        color: #222;
        font-size: 28px;
        margin-top: 0;
      }
      .active {
        color: yellowgreen;
      }
      .van-grid-item__icon-wrapper {
        position: unset;
      }
    }
  }
  /deep/ .my-grid {
    .grid-item {
      .van-icon-clear {
        position: absolute;
        right: -10px;
        top: -10px;
        font-size: 30px;
        color: #cacaca;
        z-index: 2;
      }
    }
  }

  /deep/ .recommend-channel {
    .grid-item {
      .van-grid-item__content {
        flex-direction: row;
        .van-icon-plus {
          font-size: 28px;
          margin-right: 6px;
        }
      }
    }
  }
}
</style>
